<!DOCTYPE html>
<html>
<!---需要技术支持联系QQ：44312658--->
<head>
    <meta charset="utf-8">
    <title>注册界面</title>
    <link rel="stylesheet" href="css/reset.css"/>
    <link rel="stylesheet" href="css/common.css"/>
    <link rel="stylesheet" href="css/font-awesome.min.css"/>

    <style>
        .login_form form div input {
            color: #666;
        }
    </style>
</head>
<body>
<div class="wrap login_wrap" id="app">
    <div class="content">
        <div class="logo"></div>
        <div class="login_box">
            <div class="login_form">
                <div class="login_title">
                    注册
                </div>
                <form action="#" method="post">
                    <div class="form_text_ipt">
                        <input name="username" v-model="regUser.identify" type="text" placeholder="手机号/邮箱">
                    </div>
                    <div class="send_msg_bt">
                        <!--
                        <input type="button" @click="sendMsg">发送验证码</input>
                        -->
                        <el-button style="width: 300px" @click="sendMsg" plain :disabled="msgButtonEnabled">{{msgButtonTitle}}</el-button>
                    </div>
                    <div class="ececk_warning"><span>手机号/邮箱不能为空</span></div>
                    <div class="form_text_ipt">
                        <input name="password" v-model="regUser.password" type="password" placeholder="密码">
                    </div>
                    <div class="ececk_warning"><span>密码不能为空</span></div>
                    <div class="form_text_ipt">
                        <input name="repassword" v-model="regUser.confirmPass" type="password" placeholder="重复密码">
                    </div>
                    <div class="ececk_warning"><span>密码不能为空</span></div>
                    <!--
                    <div style="text-align: center">
                        <img  src="img/randomCode.jpg"/>
                    </div>
                    <div class="form_text_ipt">
                        <input name="code" type="text" placeholder="验证码">
                    </div>
                    <div class="ececk_warning"><span>验证码不能为空</span></div>
                    -->
                    <div class="form_text_ipt">
                        <input name="code" v-model="regUser.randomCode" type="text" placeholder="验证码">
                    </div>
                    <div class="form_btn">
                        <button type="button" @click="doReg">注册</button>
                    </div>
                    <div class="form_reg_btn">
                        <span>已有帐号？</span><a href="login.html">马上登录</a>
                    </div>
                </form>

                <div style="text-align: center">
                    <span><a href="index.html">返回首页</a></span>
                    <br/>
                    <br/>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="js/include/libs.js"></script>

<script>
    if(sessionStorage.getItem("status.xiaomimall")){
        let flag = JSON.parse(sessionStorage.getItem("status.xiaomimall"));
        if(flag){
            window.location.href='index.html';
        }
    }
</script>
<!--引入前端nanoid令牌生成工具类库-->
<script type="module">
    //引入前端nanoid令牌生成工具类库
    import {nanoid} from "./js/util/nanoid.js";

    //为了实现防抖，创建一个定时器对象
    let timer ;
    //创建一个vue对象
    let vm = new Vue({
        el: '#app',
        data: {
            regUser: {
                identify: '',
                mobile: '',
                email: '',
                password: '',
                confirmPass: '',
                randomCode: ''
            }, //要注册的用户对象
            token: nanoid(16),
            msgButtonEnabled: false,
            msgButtonTitle: '发送验证码'
        },
        mounted() {
            //this.token = sessionStorage.getItem('client-token');
            console.log('---------前端生成的token是--------');
            console.log(this.token);
        },
        methods: {
            //给手机或者邮箱发送验证码的方法
            async sendMsg() {
                console.log('执行发送验证码....');
                if (!outils.isPhoneNum(this.regUser.identify) && !outils.isEmail(this.regUser.identify)) {
                    console.log('注册信息格式非法....');
                    this.$message({
                        message: '注册信息格式非法!',
                        type: 'error'
                    });
                    return;
                }

                //再检查该手机或者邮箱是否已经注册
                let result = await checkIdentifyIsReg(this.regUser.identify).then(resp=>{
                    return resp.data.code;
                })

                if(result === 400){
                    this.$message({
                        message: '该手机或者邮箱已经被注册！',
                        type: 'error'
                    });
                    return; //啥也不做了。。。。。
                }

                let params = {
                    identify: this.regUser.identify,
                    token: this.token
                };
                console.log('---------发送短信验证码时要传递的参数---------');
                console.log(params);

                //这里准备发送验证码，实现防抖功能；
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(() => {
                    sendRadomCodeMsg(params).then(resp => {
                        //console.log(resp);
                        if (resp.data.code === 200) {
                            this.$message({
                                message: '验证码发送成功!',
                                type: 'success'
                            });
                            let counter = 60;
                            this.msgButtonEnabled = true; //发送验证码的按钮不可用。
                            //'重新发送验证码(60)'
                            this.msgButtonTitle = "重新发送验证码("+counter+")"
                            //倒计时60秒之后，再可用
                            let intervalTimer = setInterval(()=>{
                                counter--;
                                if(counter===0){
                                    clearInterval(intervalTimer); //清除定时器；
                                    this.msgButtonEnabled =false;
                                    this.msgButtonTitle = '发送验证码';
                                }else{
                                    this.msgButtonTitle = "重新发送验证码("+counter+")"
                                }
                            },1000)

                        } else {
                            this.$message({
                                message: '验证码发送失败!',
                                type: 'error'
                            });
                        }
                    })

                }, 2000)

            },
            //注册时的表单验证
            regFormValidate() {

                if (!outils.isPhoneNum(this.regUser.identify) && !outils.isEmail(this.regUser.identify)) {
                    console.log('注册信息格式非法....');
                    this.$message({
                        message: '注册信息格式非法!',
                        type: 'error'
                    });
                    return false;
                }
                if (this.regUser.password.length < 6) {
                    console.log('密码长度小于6位....');
                    this.$message({
                        message: '密码长度小于6位!',
                        type: 'error'
                    });
                    return false;
                }

                if (this.regUser.password !== this.regUser.confirmPass) {
                    console.log('密码与确认密码不一致！');
                    this.$message({
                        message: '密码与确认密码不一致！',
                        type: 'error'
                    });
                    return false;
                }
                return true;
            },

            //检查验证码是否正确
            async checkRandomCode() {
                let params = {
                    token: this.token,
                    code: this.regUser.randomCode
                }

                return await validateRandomCode(params).then(resp => {
                    console.log("检查验证码是否正确返回的状态码是：" + resp.data.code)
                    return resp.data.code === 200;
                });
            },

            async doReg() {
                //表单验证
                if (!this.regFormValidate()) {
                    console.log('表单验证不通过....')
                    return;
                }

                //检查验证码是否正确
                if (!await this.checkRandomCode()) {
                    this.$message({
                        message: '验证验证失败！',
                        type: 'error'
                    });
                    return;
                }

                console.log('执行用户注册....');
                console.log('-----要注册的用户信息如下-------');
                if (outils.isPhoneNum(this.regUser.identify)) {
                    this.regUser.mobile = this.regUser.identify;
                }
                if (outils.isEmail(this.regUser.identify)) {
                    this.regUser.email = this.regUser.identify;
                }

                console.log(this.regUser);
                //打算发送异步请求，$.ajax一回事。

                regUser(this.regUser).then(resp => {
                    //console.log(resp);
                    if (resp.data.code === 200) {
                        this.$message({
                            message: resp.data.msg,
                            type: 'success'
                        });
                        //起一个定时器，一秒钟之后跳转到商城首页
                        setTimeout(() => {
                            window.location.href = 'login.html'
                        }, 1000);
                    }
                })
            }
        }
    })
</script>
</body>
</html>
